﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Huirui.Cavan.Application.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SalesModule;
using System.Linq;
using AutoMapper;
using Huirui.Cavan.Domain.Seedwork;

namespace Huirui.Cavan.Presentation.Controller.SalesModule
{
	#region OtherReceivable

    public class ReceivablesController : EntityController<Receivables, long, ReceivablesSearch, IReceivablesViewModel, IReceivablesAppService>, IReceivablesController
    {
        public override string ControllerName
        {
            get { return ControllerConst.RECEIVABLES; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public ReceivablesController()
        {
            this.Model = ModelFactory.CreateModel<IReceivablesViewModel>();
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "receivableses_query")]
        public JsonResult GetReceivableses(ReceiptViewModel model)
        {
            ReceivablesJsonViewModel json = new ReceivablesJsonViewModel();
            json.rows = new List<ReceivablesJson>();

            IList<Receivables> receivableses = this.AppService.GetBySearch(SearchFactory.CreateReceivablesSearch(model.ReceivablesSearch.StartDate, model.ReceivablesSearch.EndDate, model.ReceivablesSearch.ReceivablesCode, model.ReceivablesSearch.CustomerCode)).ToList();
            foreach (Receivables r in receivableses)
            {
                ReceivablesJson rj = Mapper.Map<Receivables, ReceivablesJson>(r);
                json.rows.Add(rj);
            }
            json.total = receivableses.Count;

            return Json(json, "application/json");
        }

        [ActionName("listbytype")]
        [ActionPermissionAttribute(Name = "receivableses_query")]
        public JsonResult GetReceivableses(int type)
        {
            ReceivablesJsonViewModel json = new ReceivablesJsonViewModel();
            json.rows = new List<ReceivablesJson>();

            IList<Receivables> receivableses = this.AppService.GetBySearch(SearchFactory.CreateReceivablesSearch(type, string.Empty)).ToList();
            foreach (Receivables r in receivableses)
            {
                ReceivablesJson rj = Mapper.Map<Receivables, ReceivablesJson>(r);
                json.rows.Add(rj);
            }
            json.total = receivableses.Count;

            return Json(json, "application/json");
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "receivableses_edit")]
        public ActionResult AddCustomerInitialBalance()
        {
            return View("~/Views/System/v1/addCustomerInitialBalance.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "receivableses_edit")]
        public JsonResult AddCustomerInitialBalance(ReceivablesViewModel model)
        {
            BoolResult jsonResult = null;

            model.Item.ReceivablesType = (SalesIdentifier.ReceivablesType)model.Type;
            BoolGenericResult<Receivables> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
	#endregion
}